
use "[Folder]\Firm_Labor_Intermediate.dta"

replace EffUnits=EffUnits*tjomf_y_av

///////////////////////////////////////////////////////////////////////////////////////////
//Adding "cultural characteristics"////////////////////////////////////////////////////////
merge 1:1 LopNr_PersonNr year using "[Folder]\OccSort_Data.dta", keepusing(FodelseLandnamnMor FodelseLandnamnFar FodelseLandnamnMor FodelseLandnamnFar Y003_CF Y003_CM Y003 WVInd* )
drop if _merge==2
drop _merge

//Re-scale so that "labor types" don't turn negative. And to have cross-country stdv=1 for autonomy measure
replace Y003=(Y003+0.934001)/0.514
replace Y003_CF=(Y003_CF+0.934001)/0.514
replace Y003_CM=(Y003_CM+0.934001)/0.514

egen Y003_P = rowmean(Y003_CF Y003_CM ) 
replace Y003_P = 0 if  (FodelseLandnamnMor==107 | FodelseLandnamnMor==. ) & (FodelseLandnamnFar==107 | FodelseLandnamnFar==. )

replace Y003=0 if WVInd!=1
replace Y003_P=0 if WVInd_CF!=1 & WVInd_CM!=1

///////////////////////////////////////////////////////////////////////////////////////////


//Splitting workers into migrants, sec.gen. and natives
gen MigCat = 3 
replace MigCat = 1 if FodelseLandnamn!=107
replace MigCat = 2 if FodelseLandnamn==107 & ( (FodelseLandnamnFar!=107 & FodelseLandnamnFar!=.)   | (FodelseLandnamnMor!=107 & FodelseLandnamnMor!=.) )
gen WVSSwe=0
replace WVSSwe=1 if missing(WVInd_CF) & FodelseLandnamnMor==107
replace WVSSwe=1 if missing(WVInd_CM) & FodelseLandnamnFar==107
replace MigCat = 3 if MigCat==2 & WVSSwe==1


gen EU_Nat = EffUnits
replace EU_Nat =0 if MigCat!=3

gen EU_Sec = EffUnits
replace EU_Sec =0 if MigCat!=2

gen EU_Mig = EffUnits
replace EU_Mig =0 if MigCat!=1


gen EU_Y003 = EffUnits*Y003
replace EU_Y003 =0 if MigCat!=1

gen EU_Y003_P = EffUnits*Y003_P
replace EU_Y003_P =0 if MigCat!=2 


keep LopNr_PeOrgNr year u  KU1Ink  EffUnits EU_Nat EU_Sec  EU_Mig EU_Y003 EU_Y003_P tjomf_y_av ChildMig FodelseLandnamn Kon MigCat Y003 Y003_P    



collapse (sum) KU1Ink  EffUnits EU_Nat EU_Sec EU_Mig EU_Y003 EU_Y003_P (count) countKU1=KU1Ink (max) u=u ,  by(LopNr_PeOrgNr year Kon )

sort LopNr_PeOrgNr year  
by LopNr_PeOrgNr year: egen totcount = total(countKU1)


	gen count_M = 0
	gen count_F = 0
	replace count_M=countKU1 if Kon==0
	replace count_F=countKU1 if Kon==1
	
		
	gen countM = 0
	gen countF = 0
	replace countM=countKU1 if Kon==0  
	replace countF=countKU1 if Kon==1  

	gen EU_Nat_M = 0 
	replace EU_Nat_M = EU_Nat if  Kon==0  
	gen EU_Mig_M = 0 
	replace EU_Mig_M = EU_Mig if  Kon==0  
	gen EU_Sec_M = 0 
	replace EU_Sec_M = EU_Sec if  Kon==0  
	
	gen EU_Y003_M = 0 
	replace EU_Y003_M = EU_Y003 if  Kon==0  
	
	gen EU_Y003_P_M = 0 
	replace EU_Y003_P_M = EU_Y003_P if  Kon==0  

	gen EU_Nat_F = 0 
	replace EU_Nat_F = EU_Nat if  Kon==1  
	gen EU_Mig_F = 0 
	replace EU_Mig_F = EU_Mig if  Kon==1  
	gen EU_Sec_F = 0 
	replace EU_Sec_F = EU_Sec if  Kon==1  
		

drop countKU1	
collapse (max) count* totcount E* u, by(LopNr_PeOrgNr year)


merge 1:1 LopNr_PeOrgNr year using  "[FOLDER]\Panel_Firm.dta"

//Drop obs. w/o a firm				
drop if _merge!=3
drop _merge



save "[Folder]\Panel_Firm_Lab_DirVarInd.dta" , replace


gen totcount2=countM + countF 

		gen EU_Nat_A = (EU_Nat_M) *  3.601327  //number for Swe after "replace Y003=(Y003+0.934001)/0.514", i.e. setting to min 0 stdv 1. 

		gen RefGr = EU_Nat_M 

		gen lab_FNat = EU_Nat_F + EU_Sec_F
		gen lab_FFor = EU_Mig_F	

		gen lab_M_1 = EU_Sec_M
		gen lab_M_2 = EU_Mig_M

		gen lab_M_C1 = EU_Y003_P_M
		gen lab_M_C2 = EU_Y003_M
		gen lab_M_C3 = EU_Nat_A 


gen logFtg_Foradlingsvarde=log(Ftg_Foradlingsvarde)

								
//Create needed vars for firm regressions:

rename Ftg_Summa_bruttoinv GrosInvTot
gen logGrosInvTot = log(GrosInvTot)
gen logGrosInvTot_Sq = logGrosInvTot^2
gen logGrosInvTot_Qu = logGrosInvTot^3
  

gen Cap =  Ftg_Anlaggningstillgangar
gen logCap=log(Cap)
gen logCap_Sq=logCap^2
gen logCap_Qu=logCap^3

rename totcount FirmSize
drop if FirmSize<6				
gen FirmSize_1=0
replace FirmSize_1=1 if FirmSize<16 & !missing(FirmSize) 
gen FirmSize_2=0
replace FirmSize_2=1 if FirmSize>15 & FirmSize<51 & !missing(FirmSize)
gen FirmSize_3=0
replace FirmSize_3=1 if FirmSize>50 & FirmSize<151 & !missing(FirmSize)
gen FirmSize_4=0
replace FirmSize_4=1 if FirmSize>150  & FirmSize<501 & !missing(FirmSize)
gen FirmSize_5=0
replace FirmSize_5=1 if FirmSize>500  & !missing(FirmSize)


tab year, gen(y_)

bysort Org_BelKommun: egen k_Size=sum(totcount)
sort k_Size
egen k_id =group(k_Size)
sum k_id
scalar maxid=r(max)
//City fixed effects; number 42 chosen to include FE for all counties with >50k inhabitants (the smallest urban area with a FE will have around 27k inhabitants). 
tab Org_BelKommun if k_id>maxid-42 ,gen(k_)
recode k_* (.=0)
gen Ind = substr(Org_Sni2007G,2,.)
destring Ind, replace
tab Ind, gen(Ind)





save "[Folder]\Panel_Firm_Lab_DirVarInd_temp.dta" , replace


